<?php

namespace app\api\controller;

use app\admin\model\Feature;
use app\common\controller\Api;
use Carbon\Carbon;
use app\common\library\Yunding;
use app\admin\model\Cleanroom;

/**
 * 首页接口
 */
class Room extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function baseinfo()
    {
        $id = $this->request->get('id', null);
        $room = \app\common\model\Room::with('store')->find($id);
        /*
        if ($room->status != 'normal') {
            $this->error('该房间已下线');
        }
        */

        $room->room_bannerimage = ykxImageUrl($room->room_bannerimage);
        $room->room_slideimages = (function ($value) {
            $rows = explode(',', $value);
            return array_map(function ($e) {
                return ykxImageUrl($e);
            }, $rows);
        })($room->room_slideimages);

        $roomReservationCount = \app\admin\model\Reserve::where('room_id', $id)->where('starttime', '>', Carbon::now()->subMonth(3)->toDateTimeString())->count();
        $roomReservationCount = $roomReservationCount > 20 ? $roomReservationCount : '20+';

        $room->promotion_rent_info = "近期有{$roomReservationCount}次预定";
        $room->features = (function ($ids) {
            $features = Feature::whereIn('id', $ids)->select();
            return array_map(function ($e) {
                return [
                    'id' => $e->id,
                    'title' => $e->feature_name,
                    'color' => $e->color,
                    'icon_url' => ykxImageUrl($e->iconimage),
                ];
            }, $features);
        })($room->feature_ids);

        $room->store_name = $room->store->store_name;

        $this->success('OK',  $room);
    }

    public function getCleanRoomList()
    {
        $user = $this->auth->getUser();
        if (in_array($user->group_id, [4]) or $user->id == 5077) { // 5077 是 我
            $roomlist = model('Room')->with('store')->where('status', 'normal')->select();
            $roomlist = array_map(function($room) {
                return [
                    'id' => $room->id,
                    'room_name' => $room->room_name,
                    'store_name' => $room->store->store_name,
                    'room_bannerimage' => ykxImageUrl($room->room_bannerimage),
                    'latitude' => $room->store->store_latitude,
                    'longitude' => $room->store->store_longitude,
                ];
            }, $roomlist);
        } else {
            $roomlist = [];
        }
        $this->success('OK', $roomlist);
    }

    public function openElectric() {
        $id = $this->request->get('room_id', null);
        $room = \app\common\model\Room::find($id);
        if (empty($room->yunding_homeid) or empty($room->yunding_elemetor_uuid)) {
            $this->error('该教室未配置云顶账号');
        }
        $yunding = new Yunding();
        $yunding->elemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid);
        if (!empty($room->yunding_elemetor_uuid2)) {
            $yunding->elemeterSwitchOn($room->yunding_homeid, $room->yunding_elemetor_uuid2);
        }

        Cleanroom::create([
            'room_id' => $id,
            'user_id' => $this->auth->id,
            'opentime' => Carbon::now()->toDateTimeString(),
            'closetime' => Carbon::now()->addMinutes(30)->toDateTimeString(),
            'createtime' => Carbon::now()->timestamp,
            'updatetime' => Carbon::now()->timestamp,
        ]);

        $this->success('开电成功');
    }

}
